limsJudgmentBasis.editPage = {

    getGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'judgment-basis-edit-list-grid',
            url : limsJudgmentBasis.baseUrl + '/queries/raw',
            service : 'limsJudgmentBasisServiceImpl',
            dbTable : 'T_LIMS_JUDGMENT_BASIS',
            columns : limsJudgmentBasis.getEditListColumns(),
            page : true,
            deleteFormatter: function (row) {
                return row.judgCode || row.id;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'judgment-testmethod-edit-list-grid', 'tm-analyte-edit-list-grid', 'analyte-highlow-edit-list-grid' ]);
                }
            },
            onRowActive : function(index, row) {
                Gikam.getComp('judgment-testmethod-edit-list-grid').refresh({
                    url: limsJudgmentBasis.judgMethodUrl + '/queries',
                    requestData: {
                        judgId_EQ: row.id
                    }
                });
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    Gikam.preInsert({
                        modalTitle : 'GIKAM.BUTTON.INSERT',
                        fields : limsJudgmentBasis.getBaseInfoFormFields(),
                        url : limsJudgmentBasis.baseUrl,
                        titleWidth : 100
                    }).done(function(id) {
                        Gikam.getComp('judgment-basis-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('judgment-basis-edit-list-grid').deleteRows(limsJudgmentBasis.baseUrl);
                }
            } ]
        }
    },

    getJudgmentTestMethodGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'judgment-testmethod-edit-list-grid',
            service : 'limsJudgmentBasisTestmethodServiceImpl',
            dbTable : 'T_LIMS_JUDGMENT_BASIS_TESTMETHOD',
            columns : limsJudgmentBasis.getJudgmentTestMethodListColumns(),
            deleteFormatter: function (row) {
                return row.testCode || row.id;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'tm-analyte-edit-list-grid', 'analyte-highlow-edit-list-grid' ]);
                }
            },
            onRowActive : function(index, row) {
                Gikam.getComp('tm-analyte-edit-list-grid').refresh({
                    url: limsJudgmentBasis.judgAnalyteUrl + '/queries',
                    requestData: {
                        judgTestId_EQ: row.id
                    }
                });
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    let row = Gikam.getComp('judgment-basis-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(row)) {
                        return;
                    }
                    Gikam.create('modal', {
                        title : 'GIKAM.BUTTON.INSERT',
                        url : IFM_CONTEXT + '/secure/basemodule/coredata/tests/page/test-method-choose-list' + Gikam.param({
                            target_filter : encodeURI(JSON.stringify({
                                type : 'unassigned ',
                                targetTable : 'T_LIMS_JUDGMENT_BASIS_TESTMETHOD',
                                targetMatchColumn : 'TESTMETHODID',
                                filter : [{
                                    targetFilterColumn : 'judgId',
                                    targetFilterValue : row.id
                                }]
                            })),
                        }),
                        onAfterClose : function(rows) {
                            if (Gikam.isEmpty(rows)) {
                                return;
                            }
                            var chooseData = rows.map(function (item) {
                                return {
                                    judgId: row.id,
                                    testMethodId: item.id,
                                    testId: item.testId,
                                    testCode: item.ext$.testcode,
                                    methodCode: item.ext$.methodcode
                                }
                            });
                            Gikam.getComp('judgment-testmethod-edit-list-grid').insert(limsJudgmentBasis.judgMethodUrl, chooseData).done(function () {
                                Gikam.getComp('judgment-testmethod-edit-list-grid').refresh();
                            });

                        }
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('judgment-testmethod-edit-list-grid').deleteRows(limsJudgmentBasis.judgMethodUrl);
                }
            } ]
        }
    },

    getJudgmentAnalyteGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'tm-analyte-edit-list-grid',
            service : 'limsJudgmentBasisTmAnalyteServiceImpl',
            dbTable : 'T_LIMS_JUDGMENT_BASIS_TM_ANALYTE',
            columns : limsJudgmentBasis.getJudgmentAnalyteListColumns(),
            deleteFormatter: function (row) {
                return row.analyte || row.id;
            },
            onLoadSuccess : function(data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0);
                } else {
                    Gikam.cleanCompData([ 'analyte-highlow-edit-list-grid' ]);
                }
            },
            onRowActive : function(index, row) {
                Gikam.getComp('analyte-highlow-edit-list-grid').refresh({
                    url: limsJudgmentBasis.highlowUrl + '/queries',
                    requestData: {
                        judgAnalyteId_EQ: row.id
                    }
                });
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var activedRow = Gikam.getComp('judgment-testmethod-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM');
                        return;
                    }
                    Gikam.create('modal', {
                        title: 'GIKAM.BUTTON.INSERT',
                        height: '70%',
                        width: '45%',
                        url: IFM_CONTEXT + '/secure/basemodule/coredata/tests/page/analyte-choose-list' + Gikam.param({
                            testMethodId_EQ: activedRow.testMethodId,
                            target_filter: encodeURI(JSON.stringify({
                                type: 'unassigned',
                                targetTable: 'T_LIMS_JUDGMENT_BASIS_TM_ANALYTE',
                                targetMatchColumn: 'ANALYTE',
                                thisMatchColumn: 'ANALYTE',
                                filter: [{
                                    targetFilterColumn: 'judgTestId',
                                    targetFilterValue: activedRow.id
                                }]
                            })),
                            analType_EQ: 'N'
                        }),
                        onAfterClose: function (rows) {
                            if (!Gikam.isEmpty(rows)) {
                                var chooseData = rows.map(function (item) {
                                    return {
                                        judgId: activedRow.judgId,
                                        judgTestId: activedRow.id,
                                        testMethodId: activedRow.testMethodId,
                                        analyteId: item.id,
                                        analyte: item.analyte,
                                        analType: item.analType
                                    }
                                });
                                Gikam.getComp('tm-analyte-edit-list-grid').insert(limsJudgmentBasis.judgAnalyteUrl, chooseData).done(function () {
                                    Gikam.getComp('tm-analyte-edit-list-grid').refresh();
                                });
                            }
                        }
                    })
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('tm-analyte-edit-list-grid').deleteRows(limsJudgmentBasis.judgAnalyteUrl);
                }
            } ]
        }
    },

    getJudgmentAnalyteHighLowGridParam : function() {
        var _this = this;
        return {
            type : 'grid',
            id : 'analyte-highlow-edit-list-grid',
            service : 'limsJudgmentBasisTmAnalyteHighlowServiceImpl',
            dbTable : 'T_LIMS_JUDGMENT_BASIS_TM_ANALYTE_HIGHLOW',
            columns : limsJudgmentBasis.getHighLowListColumns(),
            onUpdated: function (data) {
                if (!Gikam.isEmpty(data.judge)) {
                    this.refreshRowById(data.id);
                }
            },
            toolbar : [ {
                type : 'button',
                text : 'GIKAM.BUTTON.INSERT',
                icon : 'add',
                onClick : function() {
                    var activedRow = Gikam.getComp('tm-analyte-edit-list-grid').getActivedRow();
                    if (Gikam.isEmpty(activedRow)) {
                        Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200);
                        return false;
                    }
                    Gikam.preInsert({
                        modalTitle: 'GIKAM.BUTTON.INSERT',
                        fields: limsJudgmentBasis.getHighLowFormFields(),
                        titleWidth: 120,
                        url: limsJudgmentBasis.highlowUrl,
                        param: {
                            judgId: activedRow.judgId,
                            judgTestId: activedRow.judgTestId,
                            judgAnalyteId: activedRow.id,
                        }
                    }).done(function (id) {
                        Gikam.getComp('analyte-highlow-edit-list-grid').refresh();
                    });
                }
            }, {
                type : 'button',
                text : 'GIKAM.BUTTON.DELETE',
                icon : 'remove-row',
                onClick : function() {
                    Gikam.getComp('analyte-highlow-edit-list-grid').deleteRows(limsJudgmentBasis.highlowUrl);
                }
            } ]
        }
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-judgment-basis-edit-list-layout',
            renderTo : workspace.window.$dom,
            west : {
                width : '30%',
                title : 'T_LIMS_JUDGMENT_BASIS.SERVICE_NAME',
                items : [_this.getGridParam()]
            },
            center : {
                items : [{
                    type : 'layout',
                    north : {
                        height : '40%',
                        title : 'T_LIMS_JUDGMENT_BASIS_TESTMETHOD.SERVICE_NAME',
                        items : [_this.getJudgmentTestMethodGridParam()]
                    },
                    west : {
                        width : '50%',
                        title : 'T_LIMS_JUDGMENT_BASIS_TM_ANALYTE.SERVICE_NAME',
                        items : [_this.getJudgmentAnalyteGridParam()]
                    },
                    center : {
                        items : [{
                            type : 'layout',
                            center : {
                                title : 'T_LIMS_JUDGMENT_BASIS_TM_ANALYTE_HIGHLOW.SERVICE_NAME',
                                items : [_this.getJudgmentAnalyteHighLowGridParam()]
                            },
                        }]
                    }
                }]
            }
        });
    },

    init : function() {
        this.create();
    }
};
